<template>
<section class="list" ref="wrapper">
  <section>
    <section>
      <div class="title">您的位置</div>
      <div class="btn-list">
        <div class="btn-wrapper">
          <div class="button">{{this.currentCity}}</div>
        </div>
      </div>
    </section>
    <section>
      <div class="title">热门城市</div>
      <div class="btn-list">
        <div class="btn-wrapper"
        v-for="item of hotCities"
        :key="item.id"
        @click="handleCityClick(item.name)"
        >
          <div class="button">{{item.name}}</div>
        </div>
      </div>
    </section>
    <section v-for="(item,key) of cities" :key="key" :ref="key">
      <div class="title">{{key}}</div>
      <ul>
        <li
        v-for="innerItem of item"
        :key="innerItem.id"
        @click="handleCityClick(innerItem.name)"
        >{{innerItem.name}}</li>
      </ul>
    </section>
  </section>
</section>
</template>
<script>
import Bscroll from 'better-scroll'
import {mapState, mapMutations} from 'vuex'
export default {
  name: 'CityList',
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    handleCityClick (city) {
      // this.$store.dispatch('changeCity', city)
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl'
.list
  position: absolute
  top: 7.2rem
  left: 0
  right: 0
  bottom: 0
  overflow: hidden
.title
  background: #eee
  color: #666
  height: 1.8rem
  line-height: 1.8rem
  padding-left: .5rem
  font-size: 1.5rem
  border-bottom-style: solid
  border-bottom-color: #ccc
  border-bottom-width: .1rem
.btn-list
  padding: .2rem 3.0rem .2rem .2rem
  overflow: hidden
.btn-wrapper
  float: left
  width: 33.33%
.button
  text-align: center
  border: .1rem solid #ccc
  font-size: 1.4rem
  padding: .2rem 0
  margin: .2rem .3rem
  border-radius: .5rem
ul>li
  line-height: 2.0rem
  font-size: 1.4rem
  padding-left: .5rem
  border-bottom-style: solid
  border-bottom-color: #ccc
  border-bottom-width: .1rem
</style>
